//package com.cdinternationalcompany.syhapp
//
////import com.baidu.mapapi.SDKInitializer
//import android.app.ActivityManager
//import android.app.Application
//import android.content.Context
//import android.os.Handler
//import android.os.Process
//import android.util.Log
//import androidx.multidex.MultiDex
//import com.cdinternationalcompany.syhapp.push.Constants
//import com.cdinternationalcompany.syhapp.push.Constants.Companion.tag
//import com.cdinternationalcompany.syhapp.push.ManufacturerHelper
//import com.cdinternationalcompany.syhapp.push.TokenObserver
//import com.heytap.msp.push.HeytapPushManager
//import com.heytap.msp.push.callback.ICallBackResultService
//import com.vivo.push.IPushActionListener
//import com.vivo.push.PushClient
//import com.xiaomi.channel.commonutils.logger.LoggerInterface
//import com.xiaomi.mipush.sdk.Logger
//import com.xiaomi.mipush.sdk.MiPushClient
//
//
//class App : Application() {
//    private val mHandler = Handler()
//    override fun attachBaseContext(base: Context?) {
//        super.attachBaseContext(base)
//        MultiDex.install(this)
//    }
//
//    override fun onCreate() {
//        super.onCreate()
////        SDKInitializer.initialize(this)
//        try {
////            initPushSdk()
//        }catch (e: Exception){
//            Log.w(Constants.tag, e)
//        }
//    }
//
//    public fun initPushSdk() {
//        if (ManufacturerHelper.isOPPO()) {
//            if(!HeytapPushManager.isSupportPush(this)){
//                Log.d("Push", "Fuck not support")
//                return
//            }
//            HeytapPushManager.init(this, true)
//            HeytapPushManager.getRegisterID()
//            HeytapPushManager.register(this, "01a743f7a8e744599df8e453ba86af27", "3ba5095e7b204ef4a4d11b8cdd53eb46", object : ICallBackResultService {
//                override fun onRegister(p0: Int, p1: String?) {
//                    Log.d("Push", "$p0 registerId ---> $p1")
//                    TokenObserver.instance.collectionToken(applicationContext)
//                }
//
//                override fun onUnRegister(p0: Int) {
//                }
//
//                override fun onSetPushTime(p0: Int, p1: String?) {
//                }
//
//                override fun onGetPushStatus(code: Int, status: Int) {
//                    if (code == 0 && status == 0) {
//                        Log.d(tag, "Push状态正常，code= $code,status= $status")
//                    } else {
//                        Log.d(tag, "Push状态错误，code= $code status= $status")
//                    }
//                }
//
//                override fun onGetNotificationStatus(code: Int, status: Int) {
//                    if (code == 0 && status == 0) {
//                        Log.d(tag, "通知状态正常，code= $code,status= $status")
//                    } else {
//                        Log.d(tag, "通知状态错误, code= $code status= $status")
//                    }
//                }
//
//                override fun onError(code: Int, status: String?) {
//                    Log.d(tag, "SetPushTime，code= $code,result: $status")
//                }
//
//            })
//        } else if (ManufacturerHelper.isViVo()) {
//            PushClient.getInstance(this).initialize()
//            PushClient.getInstance(this).turnOnPush { object : IPushActionListener {
//                override fun onStateChanged(p0: Int) {
//                    Log.d(tag, "SetPushTime，code= $p0")
//                }
//            }
////                Log.d(tag, "SetPushTime，code= $it");
//
//
//
//            }
//
////            VivoPushReceiver.
//        } else if (ManufacturerHelper.isXiaoMi()) {
////            if (shouldInit()) {
//            MiPushClient.registerPush(this, "2882303761520067056", "5852006732056")
//            val newLogger: LoggerInterface = object : LoggerInterface {
//                override fun setTag(tag: String) {
//                    // ignore
//                }
//
//                override fun log(content: String, t: Throwable) {
//                    Log.d(Constants.tag, content, t)
//                }
//
//                override fun log(content: String) {
//                    Log.d(Constants.tag, content)
//                }
//            }
//            Logger.setLogger(this, newLogger)
//
////            }
//        }
////        mHandler.postDelayed({
//////                    TokenObserver.instance.collectionToken(context)
////            TokenObserver.instance.collectionToken(this)
////        }, 1000)
//
//    }
//
//    private fun shouldInit(): Boolean {
//        val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
//        val processInfos = am.runningAppProcesses
//        val mainProcessName = applicationInfo.processName
//        val myPid: Int = Process.myPid()
//        for (info in processInfos) {
//            if (info.pid == myPid && mainProcessName == info.processName) {
//                return true
//            }
//        }
//        return false
//    }
//}

package com.cdinternationalcompany.syhapp

//import com.baidu.mapapi.SDKInitializer
//import androidx.multidex.MultiDex
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Process
import android.util.Log
import androidx.multidex.MultiDex
import com.cdinternationalcompany.syhapp.push.Constants
import com.cdinternationalcompany.syhapp.push.Constants.Companion.tag
import com.cdinternationalcompany.syhapp.push.ManufacturerHelper
import com.cdinternationalcompany.syhapp.push.TokenObserver
//import com.cdinternationalcompany.syhapp.push.TokenObserver.Companion.TAG
import com.heytap.msp.push.HeytapPushManager
import com.heytap.msp.push.callback.ICallBackResultService
import com.vivo.push.PushClient
import com.vivo.push.PushConfig
import com.xiaomi.channel.commonutils.logger.LoggerInterface
import com.xiaomi.mipush.sdk.ErrorCode
import com.xiaomi.mipush.sdk.Logger
import com.xiaomi.mipush.sdk.MiPushClient


class App : Application() {

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
//        SDKInitializer.initialize(this)
//        try {
////            initPushSdk()
//        }catch (e: Exception){
//            Log.w(Constants.tag, e)
//        }
//        ThrowException()
    }

//    fun ThrowException() {
//        // 调试打印堆栈而不退出
//        Log.d("ThrowException", Log.getStackTraceString(Throwable()))
//
//        // 创建异常打印堆栈
//        val e = java.lang.Exception("this is a log")
//        e.printStackTrace()
//
//        // 获取当前线程的堆栈
//        for (i in Thread.currentThread().stackTrace) {
//            Log.i("ThrowException", i.toString())
//        }
//        val re = RuntimeException()
//        re.fillInStackTrace()
//        Log.i("ThrowException", "stackTrace", re)
//
//        // 主动抛出异常调试
//        try {
//            Log.i(
//                "ThrowException",
//                "--------------------------------NullPointerException-----------1"
//            )
//            throw NullPointerException()
//        } catch (e1: NullPointerException) {
//            // TODO: handle exception
//            Log.i("ThrowException", "--------------------------------NullPointerException")
//            Log.e("ThrowException", Log.getStackTraceString(e1))
//            // e1.printStackTrace();
//        }
//        Log.i(
//            "ThrowException",
//            "--------------------------------NullPointerException-----------end"
//        )
//    }

    fun initPushSdk() {
        if (ManufacturerHelper.isOPPO()) {
            if (!HeytapPushManager.isSupportPush(applicationContext)) {
                Log.d("Push", "Fuck not support")
                return
            }
            HeytapPushManager.init(this, true)
//            HeytapPushManager.getRegisterID()
//            HeytapPushManager.register(this,"","",)

            HeytapPushManager.register(
                this,
                "6a4f6eae9ba047d0b8fe4c659ed18733",
                "f1c8b13f6330433e81f28b192902af80",
                object : ICallBackResultService {
                    override fun onRegister(p0: Int, p1: String?, p2: String?, p3: String?) {
                        Log.d("Push", "$p0 registerId ---> $p1")
                        val handler = Handler(Looper.getMainLooper())
                        handler.postDelayed({
                            TokenObserver.instance.collectionToken(applicationContext)
                        }, 2000)
                    }

                    override fun onUnRegister(p0: Int, p1: String?, p2: String?) {
                    }


                    override fun onSetPushTime(p0: Int, p1: String?) {
                    }

                    override fun onGetPushStatus(code: Int, status: Int) {
                        if (code == 0 && status == 0) {
                            Log.d(tag, "Push状态正常，code= $code,status= $status")
                        } else {
                            Log.d(tag, "Push状态错误，code= $code status= $status")
                        }
                    }

                    override fun onGetNotificationStatus(code: Int, status: Int) {
                        if (code == 0 && status == 0) {
                            Log.d(tag, "通知状态正常，code= $code,status= $status")
                        } else {
                            Log.d(tag, "通知状态错误, code= $code status= $status")
                        }
                    }

                    override fun onError(code: Int, status: String?, p2: String?, p3: String?) {
                        Log.d(tag, "SetPushTime，code= $code,result: $status")
                    }

                })
        } else if (ManufacturerHelper.isViVo()) {
            val config = PushConfig.Builder()
                .agreePrivacyStatement(true)
                .build()
            PushClient.getInstance(this).initialize(config)
            PushClient.getInstance(this).turnOnPush {
            }
        } else if (ManufacturerHelper.isXiaoMi()) {
            if (isMainProcess()) {
//            this.applicationContext
                MiPushClient.registerPush(this, "2882303761520389580", "5712038966580")
//                MiPushClient.registerPush(this,"","",)
            }
            val newLogger: LoggerInterface = object : LoggerInterface {
                override fun setTag(tag: String) {
                    // ignore
                    Log.d(Constants.tag, tag)
                }

                override fun log(content: String, t: Throwable) {
                    Log.d(Constants.tag, content, t)
                }

                override fun log(content: String) {
                    Log.d(Constants.tag, content)
                }

            }
//            ErrorCode.
            Logger.setLogger(this, newLogger)

//            val id = MiPushClient.getRegId(this)
//            Log.d("iaomi Register id", "Xiaomi Register id -> $id")
//            val handler = Handler(Looper.getMainLooper())
//            handler.postDelayed({
////                MiPushClient.reportMessageClicked(this,)
////                MiPushClient.onReceiveRegisterResul
////                val id = MiPushClient.getRegId(this)
////                Log.d("iaomi Register id", "Xiaomi Register id -> $id")
//                TokenObserver.instance.collectionToken(this)
//            }, 2000)
        } else if (ManufacturerHelper.isHuaWei()) {

            TokenObserver.instance.collectionToken(this)
        }

    }

    private fun isMainProcess(): Boolean {
        val am = (getSystemService(ACTIVITY_SERVICE) as ActivityManager)
        val processInfos = am.runningAppProcesses
        val mainProcessName = packageName
        val myPid = Process.myPid()
        for (info in processInfos) {
            if (info.pid == myPid && mainProcessName == info.processName) {
                return true
            }
        }
        return false
    }

    private fun shouldInit(): Boolean {
        val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val processInfos = am.runningAppProcesses
        val mainProcessName = applicationInfo.processName
        val myPid: Int = Process.myPid()
        for (info in processInfos) {
            if (info.pid == myPid && mainProcessName == info.processName) {
                return true
            }
        }
        return false
    }
}